package l1j.server.server.model.damage.hit;

import l1j.server.server.ActionCodes;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.utils.Random;

public class L1Hit_Pc_Npc extends L1Hit {
	private L1PcInstance _attacker;
	private L1NpcInstance _target;
	private L1PcHit _pcHit;

	public L1Hit_Pc_Npc(L1PcInstance attacker, L1NpcInstance target) {
		_attacker = attacker;
		_target = target;
		_pcHit = new L1PcHit(attacker, target);
	}

	public boolean calcHit() {
		_pcHit.doAttackAction();
		if (_target.isInvincible())
			return false;
		if (_pcHit.calcPcHit()) {
			calcPcNpcHit();
		}
		if(_isHit){
			_target.broadcastPacket(new S_DoActionGFX(
					_target.getId(), ActionCodes.ACTION_Damage));
		}
		return _isHit;
	}

	private void calcPcNpcHit() {
		_hitRate = _pcHit.calcPcHitRate(_hitRate);
		L1OnNpcHit onh = new L1OnNpcHit(_target);
		_hitRate = onh.hitRateOnNpc(_hitRate);
		_isHit = Random.chance100(_hitRate);

		L1ItemInstance weapon = _attacker.getWeapon();
		if (weapon != null) {
			if (weapon.isKiringku()) {
				_hitRate = 100;
				_isHit = true;
			}
		}

		// 特定狀態下才可攻擊 NPC
		if (_attacker.isAttackMiss(_attacker, _target.getNpcTemplate()
				.get_npcId())) {
			_hitRate = 0;
			_isHit = false;
		}

	}

}
